#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define NUM 4  //出题数量

void Problem(int rank);  //出题函数 

int main(){
	int rank;  //难度等级 
	int option;  //选择变量 
	int exit_flag=1;  //判断是否继续答题 
	printf("请选择题目难度等级(1-14): ");
	scanf("%d",&rank);
	printf("难度等级为%d的题目如下:\n\n",rank);
	while(exit_flag){  //exit_flag为1则继续答题 
	      Problem(rank);  //出题 
		  printf("\n1: 继续答题\n2: 退出答题\n输入你的选择:");
repeat0:  scanf("%d",&option);
	      if(option==1){
	      	printf("\n请选择题目难度等级(1-14):");  //继续答题 
		    scanf("%d",&rank);
	      } 
	      else if(option==2){
	      	exit_flag=0;
	      }
		  else{ 
            printf("输入不合法，请重新输入(1-14): ");
		    goto repeat0;
		  }
	}
	return 0;
}

void Problem(int rank){
	int i;
	int option;  //选择变量
    int a[NUM],b[NUM],c[NUM],d[NUM],e[NUM],cor_result[NUM],stu_result[NUM];  //a[]-e[]存放操作数，cor_result[]存放正确答案,stu_result[NUM]存放学生答案 
	srand(time(NULL));
	for(i=0;i<NUM;i++){  //随机产生操作数
		switch(rank){
			case 1: 
			case 4:
			case 7:
			case 9:
			case 11:
			case 13:
				   a[i]=rand()%10;
				   b[i]=rand()%10;
				   c[i]=rand()%10;
				   d[i]=rand()%10;
				   e[i]=rand()%10;
				   break;
			case 2:
			case 5:
			case 8:
			case 10:
			case 12:
			case 14:
				   a[i]=rand()%100;
				   b[i]=rand()%100;
				   c[i]=rand()%100;
				   d[i]=rand()%100;
				   e[i]=rand()%100;
				   break;
			case 3:
			case 6:
			       a[i]=rand()%1000;
				   b[i]=rand()%1000;
				   c[i]=rand()%1000;
				   break;	   		   
			default: break;	   
		}
	} 
repeat1:
    for(i=0;i<NUM;i++){
    	switch(rank){
    		case 1:
    		case 2:
			case 3: 
    		        printf("%2d: %d+%d\n",i+1,a[i],b[i]);  //输出题目 
		            cor_result[i]=a[i]+b[i];  //计算正确答案
	                i++;
	                if(i==NUM) break;
	                printf("%2d: %d-%d\n",i+1,a[i],b[i]);
	                cor_result[i]=a[i]-b[i];
	                break;
	        case 4:
	        case 5:
	        case 6:				        	
	        	    printf("%2d: %d+%d-%d\n",i+1,a[i],b[i],c[i]);  //输出题目 
		            cor_result[i]=a[i]+b[i]-c[i];  //计算正确答案
	                i++;
	                if(i==NUM) break;
	                printf("%2d: %d-%d+%d\n",i+1,a[i],b[i],c[i]);
	                cor_result[i]=a[i]-b[i]+c[i];
	                break;
	        case 7:
	        case 8:
			        printf("%2d: %dx%d\n",i+1,a[i],b[i]);  //输出题目 
		            cor_result[i]=a[i]*b[i];  //计算正确答案
                    i++;	    
					if(i==NUM) break;    
	        	    printf("%2d: %d/%d\n",i+1,a[i],b[i]);  //输出题目 
		            cor_result[i]=a[i]/b[i];  //计算正确答案
		            break;
		    case 9:
			case 10:	        
	        	    printf("%2d: %dx%d/%d\n",i+1,a[i],b[i],c[i]);  //输出题目 
		            cor_result[i]=a[i]*b[i]/c[i];  //计算正确答案
	        	    i++;
	        	    if(i==NUM) break;
	        	    printf("%2d: %d/%dx%d\n",i+1,a[i],b[i],c[i]);  //输出题目 
		            cor_result[i]=a[i]/b[i]*c[i];  //计算正确答案
		            break;
		    case 11:
			case 12:	        
	        	    printf("%2d: %dx%d+%d\n",i+1,a[i],b[i],c[i]);  //输出题目 
		            cor_result[i]=a[i]*b[i]+c[i];  //计算正确答案
	        	    i++;
	        	    if(i==NUM) break;
	        	    
	        	    printf("%2d: %d+%dx%d\n",i+1,a[i],b[i],c[i]);  //输出题目 
		            cor_result[i]=a[i]+b[i]*c[i];  //计算正确答案
		            i++;
	        	    if(i==NUM) break;
	        	    
		            printf("%2d: %dx%d-%d\n",i+1,a[i],b[i],c[i]);  //输出题目 
		            cor_result[i]=a[i]*b[i]-c[i];  //计算正确答案
	        	    i++;
	        	    if(i==NUM) break;
	        	    
	        	    printf("%2d: %d-%dx%d\n",i+1,a[i],b[i],c[i]);  //输出题目 
		            cor_result[i]=a[i]-b[i]*c[i];  //计算正确答案
		            i++;
	        	    if(i==NUM) break;
	        	    
	        	    printf("%2d: %d/%d+%d\n",i+1,a[i],b[i],c[i]);  //输出题目 
		            cor_result[i]=a[i]/b[i]+c[i];  //计算正确答案
	        	    i++;
	        	    if(i==NUM) break;
	        	    
	        	    printf("%2d: %d+%d/%d\n",i+1,a[i],b[i],c[i]);  //输出题目 
		            cor_result[i]=a[i]+b[i]/c[i];  //计算正确答案
		            i++;
	        	    if(i==NUM) break;
	        	    
		            printf("%2d: %d/%d-%d\n",i+1,a[i],b[i],c[i]);  //输出题目 
		            cor_result[i]=a[i]/b[i]-c[i];  //计算正确答案
	        	    i++;
	        	    if(i==NUM) break;
	        	    
	        	    printf("%2d: %d-%d/%d\n",i+1,a[i],b[i],c[i]);  //输出题目 
		            cor_result[i]=a[i]-b[i]/c[i];  //计算正确答案
		            break;        
		    case 13:
			case 14:	        
	        	    printf("%2d: %d+%dx%d-%d/%d\n",i+1,a[i],b[i],c[i],d[i],e[i]);  //输出题目 
		            cor_result[i]=a[i]+b[i]*c[i]-d[i]/d[i];  //计算正确答案
	        	    i++;
	        	    if(i==NUM) break;
	        	    
	        	    printf("%2d: %d-%d/%d+%dx%d\n",i+1,a[i],b[i],c[i],d[i],e[i]);  //输出题目 
		            cor_result[i]=a[i]-b[i]/c[i]+d[i]*d[i];  //计算正确答案
	        	    i++;
	        	    if(i==NUM) break;
	        	    
		            printf("%2d: (%d+%d)x%d-%d/%d\n",i+1,a[i],b[i],c[i],d[i],e[i]);  //输出题目 
		            cor_result[i]=(a[i]+b[i])*c[i]-d[i]/d[i];  //计算正确答案
	        	    i++;
	        	    if(i==NUM) break;
	        	    
	        	    printf("%2d: %d-%d/(%d+%d)x%d\n",i+1,a[i],b[i],c[i],d[i],e[i]);  //输出题目 
		            cor_result[i]=a[i]-b[i]/(c[i]+d[i])*d[i];  //计算正确答案
	        	    break;
	        default: break;   
    	}	
	}
	printf("\n请输入你的答案:\n");
	for(i=0;i<NUM;i++){  //读取学生答案 
		printf("%2d: ",i+1);
		scanf("%d",&stu_result[i]);
	}
	printf("\n答题结果如下:\n");
	for(i=0;i<NUM;i++){   //显示答题结果 
		if(stu_result[i]==cor_result[i])
		    printf("%2d: 正确\n",i+1);
		else 
		    printf("%2d: 错误\n",i+1);
	}
	printf("\n1：重做一遍\n2: 查看答案\n3: 退出当前等级答题\n输入你的选择:");
repeat2:
	scanf("%d",&option);
	printf("\n");
    if(option==1) goto repeat1;  //1：重做一遍
    else if(option==2){          //2: 查看答案
    	printf("正确答案如下:\n"); 
    	for(i=0;i<NUM;i++){
		   printf("%2d:%d\n",i+1,cor_result[i]);
	    }
    }
    else if(option==3) return;   //3: 退出当前等级答题
    else{
    	printf("输入不合法，请重新输入(1-3): ");
    	goto  repeat2;
    }
}